class myPromise {
    //? constructor
    constructor(fn) {
        this.status = "pending"
        this.value = ''
        this.resolveArr = []
        this.rejectArr = []
        this.resolve = (value) => {
            if (this.status === 'pending') {
                this.status = 'fulfilled'
                this.value = value
                //? 状态变化时就要执行里面存储的回调了(靠的是fn里的逻辑来执行)
                this.resolveArr.forEach((callback) => {
                    callback(this.value)
                })
            }
        }
        this.reject = (error) => {
            if (this.status === 'pending') {
                this.status = 'rejected'
                this.value = error
                this.rejectArr.forEach((callback) => {
                    callback(this.value)
                })
            }
        }
        try {
            fn(this.resolve,this.reject)
        }
        catch (e) {
            this.reject(e)
        }
    }
    then(onFulfilled, onRejected) {
        //? 这里是需要处理没有传入onfulfilled和onRejected的情况！
        onFulfilled =
            typeof onFulfilled === "function" ? onFulfilled : (value) => value;
        onRejected =
            typeof onRejected === "function"
                ? onRejected
                : (err) => {
                    throw err;
                };
        const newPromise = new Promise((resolve, reject) => {
            //? 三种状态
            if (this.status === 'fulfilled') {
                try {
                    const x = onFulfilled(this.value)
                    if (x instanceof myPromise) {
                        x.then(resolve,reject)
                    }
                    else {
                        resolve(x)
                    }
                }
                catch (e) {
                    reject(e)
                }
            }
            if (this.status === 'rejected') {
                try {
                    //? 如果没有onRejected 则这里会将错误抛出 让reject捕获错误
                    const x = onRejected(this.value)

                    if (x instanceof myPromise) {
                        x.then(resolve,reject)
                    }
                    else {
                        reject(x)
                    }
                }
                catch (e) {
                    reject(e)
                }
            }
            if (this.status === 'pending') {
                this.rejectArr.push(() => {
                    try {
                        const x = onRejected(this.value)
                        if (x instanceof myPromise) {
                            x.then(resolve,reject)
                        }
                        else {
                            reject(x)
                        }
                    }
                    catch (e) {
                        reject(e)
                    }
                })
                this.resolveArr.push(() => {
                    try {
                        const x = onFulfilled(this.value)
                        if (x instanceof myPromise) {
                            x.then(resolve,reject)
                        }
                        else {
                            resolve(x)
                        }
                    }
                    catch (e) {
                        reject(e)
                    }
                })
            }
        })
        return newPromise
    }
    catch(onRejected) {
        return this.then(null, onRejected);
    }
}
new myPromise((resolve,reject) => {
    setTimeout(()=>{
        reject('1')
    },1000)
}).then((resolve) => {
    console.log('res',resolve)
}).catch((reject) => {
    console.log('rej',reject)
})
// console.log(mp);
